<?php

namespace common\helpers;

use Yii;
use yii\bootstrap\Modal;
use metronic\widgets\MetronicDynamicModal;
use metronic\widgets\MetronicModal;

/**
 * Html provides a set of static methods for generating commonly used HTML tags.
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class Html extends \yii\helpers\BaseHtml {

    /**
     * Generates an title tag.
     */
    public static function title($title, $subtitle = null, $inline = false) {
        if ($subtitle !== null) {
            $subtitle = static::tag('small', $subtitle);
        }
        $options = [
            'class' => 'cell-title'
        ];
        if ($inline) {
            static::addCssClass($options, 'cell-title-inline');
        }
        return static::tag('h2', $title . $subtitle, $options);
    }

    /**
     * Generates an title tag.
     */
    public static function colorBox($value, $default = '#ffffff', $type = 'hex') {
        $bgcolor = ColorHelper::format($value, 'hex', $default);
        $options = [
            'class' => 'color-block',
            'style' => 'background:' . $bgcolor . ';'
        ];
        return static::tag('div', '', $options);
    }

    /**
     * Generates an image tag.
     */
    public static function img($src, $options = [], $cdn = false) {
        if (!$src) {
            $cdn = false;
            $src = '@web/themes/images/undefind.jpg';
        }
        if ($cdn !== false) {
            $src = Url::attach($src);
        }
        return parent::img($src, $options);
    }

    public static function userPorlet($name, $avatar = null, $extends = null, $options = []) {
        $data = [];
        if ($avatar) {
            $data[] = static::tag('div', static::img($avatar, ['alt' => $avatar], true), [
                'class' => 'mt-avatar',
            ]);
        }
        $tag = ArrayHelper::remove($options, 'tag', 'a');
        $url = ArrayHelper::remove($options, 'url', null);
        $userinfo = static::$tag($name, $url, [
            'class' => 'mt-author-name',
            'target' => '_blank',
            'data-pjax' => 0
        ]);
        if ($extends) {
            $userinfo .= static::tag('div', $extends, [
                'class' => 'mt-author-extends'
            ]);
        }
        $data[] = static::tag('div', $userinfo, [
            'class' => 'mt-author-text'
        ]);
        return static::tag('div', implode("\n", $data), [
            'class' => 'mt-author'
        ]);
    }

    /**
     * Generates an video tag.
     * <video id="my_video_1" class="video-js vjs-default-skin" controls preload="auto" width="100%" height="320" data-setup='{"poster": "<?= $model->cover ?>"}'>
      <source src="http://7351.mpull.live.lecloud.com/live/<?php echo $model->id; ?>/desc.m3u8" type="application/x-mpegURL">
      </video>
     */
    public static function video($source = [], $options = []) {
        $sourceTag = static::tag('source', '', $source);
        return static::tag('video', $sourceTag, $options);
    }

    /**
     * Generates an dropDownMenu tag.
     */
    public static function dropDownMenu($label, $items = [], $options = []) {
        //$caret = static::tag('span', '', ['class' => 'caret']);
        $button = static::button($label, [
            'class' => 'btn green btn-sm dropdown-toggle',
            'data-toggle' => 'dropdown',
            'aria-haspopup' => true,
            'aria-expanded' => false,
        ]);
        $menus = [];
        foreach ($items as $item) {
            $label = ArrayHelper::getValue($item, 'label');
            $url = ArrayHelper::getValue($item, 'url');
            $options = ArrayHelper::getValue($item, 'options', []);
            $menus[] = static::a($label, $url, $options);
        }
        $menu = static::tag('ul', '<li>' . implode('</li><li>', $menus) . '</li>', ['class' => 'dropdown-menu']);
        return static::tag('div', $button . $menu, ArrayHelper::merge(['class' => 'btn-group'], $options));
    }

    /**
     * Modal弹窗
     */
    public static function ajaxModal($text, $url = null, $options = [], $content = []) {
        $options['label'] = $text;
        if ($url !== null) {
            $options['href'] = Url::to($url) . '#modal';
        }
        $configs = [
            'toggleButton' => $options,
            'options' => [
                'data-backdrop' => 'static',
            ]
        ];
        if (!empty($content)) {
            $configs = ArrayHelper::merge($configs, $content);
        }
        return MetronicDynamicModal::widget($configs);
    }

    /**
     * Modal弹窗
     */
    public static function normalModal($text, $url = null, $options = [], $content = []) {
        if ($url !== null) {
            $options['href'] = Url::to($url);
        }
        $options['label'] = $text;

        $toggleButton = [
            'toggleButton' => $options,
        ];
        if (empty($content)) {
            $content['closeButton'] = false;
            $content['header'] = null;
        }
        $configs = ArrayHelper::merge($toggleButton, $content);
        return MetronicModal::widget($configs);
    }

    /**
     * 模型常量Html标签
     * @param string $text 常量标签
     * @param string|null $url 地址
     * @param array $options 选项
     * @param array $content 内容
     * @return string Html Modal.
     */
    public static function bootModal($text, $url = null, $options = [], $content = []) {
        if ($url !== null) {
            $options['href'] = Url::to($url);
        }
        $options['label'] = $text;

        $toggleButton = [
            'toggleButton' => $options,
        ];
        $configs = ArrayHelper::merge($toggleButton, $content);
        return Modal::widget($configs);
    }

    /**
     * 模型常量Html标签
     * @param string $content 常量标签
     * @param array $options 配置选项
     * @param string $language 相对语言包名称
     * @return string Html label.
     */
    public static function modelConstLabel($content, $options = [], $language = 'const') {
        $labelOptions = [
            'class' => 'label label-sm'
        ];

        $language = rtrim($language, '/') . '/';
        $text = Yii::t($language . 'label', $content);
        $class = Yii::t($language . 'class', $content);
        $style = Yii::t($language . 'style', $content);
        if ($class === $content) {
            $class = 'bg-dark';
        } elseif (in_array($class, ['info', 'success', 'danger', 'warning'])) {
            $class = 'label-' . $class;
        } else {
            $class = 'bg-' . $class;
        }
        static::addCssClass($labelOptions, $class);
        if ($style !== $content) {
            static::addCssStyle($labelOptions, $style);
        }

        return static::label($text, null, ArrayHelper::merge($labelOptions, $options));
    }

    /**
     * 版权信息
     * @param array $options 配置选项
     * @return string Html tag.
     */
    public static function copyright($options = []) {
        $host = ArrayHelper::remove($options, 'host', Yii::$app->params['domain']);
        $year = ArrayHelper::remove($options, 'year', date('Y'));
        $company = ArrayHelper::remove($options, 'company', Yii::$app->params['company']);

        $tag = ArrayHelper::remove($options, 'tag', 'p');
        $template = ArrayHelper::remove($options, 'template', Yii::$app->params['copyrightInfo']);

        return self::tag($tag, strtr($template, [
            '{host}' => $host,
            '{year}' => $year,
            '{company}' => $company,
        ]), $options);
    }

    /**
     * ICP信息
     * @param array $options 配置选项
     * @return string Html tag.
     */
    public static function icp($options = []) {
        $text = ArrayHelper::remove($options, 'text', '网站备案/许可证号：');
        $icp = ArrayHelper::remove($options, 'icp', Yii::$app->params['icp']);
        $link = ArrayHelper::remove($options, 'link', true);
        if ($link) {
            $url = ArrayHelper::remove($options, 'url', 'http://www.miitbeian.gov.cn');
            $linkOptions = ArrayHelper::remove($options, 'linkOptions', []);
            $icp = self::a($icp, $url, $linkOptions);
        }
        $tag = ArrayHelper::remove($options, 'tag', 'p');
        $layout = ArrayHelper::remove($options, 'layout', '{text}{icp}');
        return self::tag($tag, strtr($layout, [
            '{text}' => $text,
            '{icp}' => $icp,
        ]), $options);
    }

    public static function icon($options = []) {
        $tag = 'i';
        if (is_array($options)) {
            $tag = ArrayHelper::remove($options, 'tag', 'i');
        } elseif (is_string($options)) {
            $options = [
                'class' => $options
            ];
        } else {
            return '';
        }
        return self::tag($tag, '', $options);
    }

    public static function activeGallery($model, $attribute, $index, $options = []) {

        $indexAttribute = $attribute . '[' . $index . ']';
        $id = static::getInputId($model, $indexAttribute);
        $path = static::getAttributeValue($model, $indexAttribute . '[path]');
        $img = static::img($path, [], true);
        $path = static::activeHiddenInput($model, $indexAttribute . '[path]');
        $listorder = static::activeTextInput($model, $indexAttribute . '[listorder]', $options);
        $description = static::activeTextarea($model, $indexAttribute . '[description]', $options);

        return self::tag('div', $img . $path . $description . $listorder, [
            'id' => $id,
        ]);
    }

}
